JavaScript in Browser: How It Works
JavaScript is a client-side scripting language that is executed in the user's browser to add interactivity and dynamic behavior to web pages.
What Happens When JavaScript Runs in the Browser?
User Requests a Web Page: The browser sends a request to the server and retrieves HTML, CSS, and JavaScript files.
HTML Parsing: The browser parses the HTML document from top to bottom.
When it encounters a <script> tag, it stops parsing HTML and executes the JavaScript code.
JavaScript Engine: The browser uses a JavaScript engine (e.g., V8 for Chrome) to compile and execute the JavaScript code.
DOM and BOM Access: JavaScript can access and manipulate the DOM (Document Object Model) and BOM (Browser Object Model).
Rendering:After JavaScript modifies the DOM, the browser repaints and reflows the page to show the updates.
<script> Tag in JavaScript
The <script> tag is used to embed or link JavaScript code within an HTML document.
// JavaScript Code Here
console.log("Hello, JavaScript in Browser!");
External JavaScript File:
<script src="script.js"></script>
Notes:
The src attribute is used to link an external JavaScript file.
Do not put content inside <script> when using src.